﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QuanLiThuVien.Source;
using QuanLiThuVien.LinkModel;

namespace QuanLiThuVien
{
	public partial class ThongTinSach
	{
        private InsertInfo m_insertInfo = new InsertInfo();
        private UpdateInfo m_updateInfo = new UpdateInfo();
        private DeleteInfo m_deleteInfo = new DeleteInfo();
        private ShowInfo m_showInfo = new ShowInfo();
        private XuLyThem m_cXuLyThem = new XuLyThem();

        private string m_sMaDauSach;
        private string m_sMaSach;
        private string m_sMaTacGia;
        private string m_sMaTheLoai;
        static bool m_bSelectData = false;
        private string m_sMaNguoiDung;

        private THUVIEN.Service1 serv = new THUVIEN.Service1();
        //private bool insertData = false;

		public ThongTinSach(string MaNguoiDung)
		{
			this.InitializeComponent();
            m_showInfo.ShowListSach(dataSach);
            addCmbTheLoai(cmbTheLoai);
            addCmbTacGia(cmbTacGia);
            m_sMaNguoiDung = MaNguoiDung;
		}

        private void GioiHanQuyenNguoiDung()
        {
            
            if (m_cXuLyThem.LoaiNguoiDung(m_sMaNguoiDung) == "NV")
            {                
                if (serv.SelectLoaiNhanVien_NhanVien(m_sMaNguoiDung) == "LNV9FD")
                {
                    bntThemSach.Visibility = Visibility.Collapsed;
                    bntSuaSach.Visibility = Visibility.Collapsed;
                }
            }
        }

        private void ClearIndex()
        {
            txtMaSach.Text = "";
            cmbTacGia.SelectedItem = null;
            cmbTheLoai.SelectedItem = null;
            txtTenSach.Text = "";
            txtNamXuatBan.Text = "";
            txtNhaXuatBan.Text = "";
            txtTriGia.Text = "";
            dtmNgayNhap.SelectedDate = null;
        }

        private bool KiemTraNhap()
        {
            if (cmbTacGia.SelectedItem == null
                || txtTenSach.Text == ""
                || cmbTheLoai.SelectedItem == null
                || txtNamXuatBan.Text == ""
                || txtNhaXuatBan.Text == ""
                || dtmNgayNhap.SelectedDate == null)
                return true;
            else return false;
        }

        #region Xu ly cac combobox
        public void addCmbTheLoai(ComboBox m_cmbTheLoai)
        {
            for (int i = 0; i < serv.ListTenTheLoai().Length; i++)
                m_cmbTheLoai.Items.Add(serv.ListTenTheLoai()[i]);
        }

        public void addCmbTacGia(ComboBox m_cmbTacGia)
        {
            for (int i = 0; i < serv.ListTenTacGia().Length; i++)
                m_cmbTacGia.Items.Add(serv.ListTenTacGia()[i]);
        }
        //private void cmbTheLoai_SelectionChanged(object sender, SelectionChangedEventArgs e)
        //{
        //    if (m_bSelectedTenSach == false)
        //    {
        //        //addCmbTenDauSach_selectedTheLoai(cmbTenSach);
        //        //cmbTheLoai.Items.Clear();
        //       // addCmbTheLoai(cmbTheLoai);
        //        m_bSelectedTheLoai = true;
        //    }
        //    else
        //    {
        //        if (m_bSelectedTheLoai == false)
        //        {
        //            addCmbTacGia_selectedDauSach(cmbTacGia);
        //           // cmbTenSach.Items.Clear();
        //           // cmbTheLoai.Items.Clear();
        //           // addCmbTenDauSach(cmbTenSach);
        //           // addCmbTheLoai(cmbTheLoai);
        //            m_bSelectedTheLoai = true;
        //        }
        //        else
        //        {
        //            txtTenSach.Text = "";
        //            cmbTacGia.SelectedItem = null;
        //            //cmbTenSach.Items.Clear();
        //            //cmbTheLoai.Items.Clear();
        //            //cmbTacGia.Items.Clear();
        //            //addCmbTacGia(cmbTacGia);
        //            //addCmbTenDauSach(cmbTenSach);
        //            //addCmbTheLoai(cmbTheLoai);
        //            m_bSelectedTenSach = false;
        //        }
        //        //m_bSelectedTenSach = false;
        //    }
        //}

       
        //private void cmbTacGia_SelectionChanged(object sender, SelectionChangedEventArgs e)
        //{
        //    if (m_bSelectedTheLoai == true || m_bSelectedTenSach == true)
        //    {
        //        addCmbTacGia_selectedDauSach(cmbTacGia);
        //        m_bSelectedTacGia = true;
        //    }
        //}
        #endregion

        private void bntThemDauSach_Click(object sender, RoutedEventArgs e)
        {
            DauSach m_themdausach = new DauSach();
            m_themdausach.Show();
        }

        private void btnRefeshCombobox_Click(object sender, RoutedEventArgs e)
        {            
            cmbTheLoai.Items.Clear();
            cmbTacGia.Items.Clear();
            addCmbTheLoai(cmbTheLoai);
            addCmbTacGia(cmbTacGia);
        }

        private void txtNamXuatBan_TextChanged(object sender, TextChangedEventArgs e)
        {
            m_cXuLyThem.IsNumber(txtNamXuatBan);
        }

        private void bntThemSach_Click(object sender, RoutedEventArgs e)
        {
            NhapSach m_NhapSach = new NhapSach();
            m_NhapSach.Show();
        }

        private void dataSach_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!m_bSelectData)
            {
                var item = dataSach.SelectedItem as DSSach;
                txtMaSach.Text = item.MaSach.ToString();
                txtTenSach.Text = item.TenDauSach.ToString();
                txtNhaXuatBan.Text = item.NhaXuatBan.ToString();
                txtNamXuatBan.Text = Int32.Parse(item.NamXuatBan.ToString()).ToString();
                txtTriGia.Text = item.DonGia.ToString();
                cmbTheLoai.Items.Clear();
                addCmbTheLoai(cmbTheLoai);
                cmbTheLoai.SelectedItem = item.TenTheLoai.ToString();
                cmbTacGia.Items.Clear();
                addCmbTacGia(cmbTacGia);
                cmbTacGia.SelectedItem = item.HoTenTacGia.ToString();
                dtmNgayNhap.SelectedDate = DateTime.Parse(item.NgayLap.ToString());

                m_sMaTheLoai = serv.SelectMaTheLoai(item.TenTheLoai.ToString());
                m_sMaDauSach = serv.SelectMaDauSach(item.TenDauSach.ToString(), m_sMaTheLoai);
                m_sMaSach = item.MaSach.ToString();
                m_sMaTacGia = serv.SelectMaTacGia(item.HoTenTacGia.ToString());

            }
            else
            {
                m_bSelectData = false;
                m_showInfo.ShowListSach(dataSach);
            }            
        }

        private void bntSuaSach_Click(object sender, RoutedEventArgs e)
        {

            if (KiemTraNhap())
                MessageBox.Show("Chưa có đầy đủ thông tin để chỉnh sửa", "Thông báo");
            else
            {
                m_updateInfo.UpdateDauSach(m_sMaDauSach, txtTenSach.Text, serv.SelectMaTheLoai(cmbTheLoai.SelectedItem.ToString()));
                m_updateInfo.UpdateSach(m_sMaSach, m_sMaDauSach, Int32.Parse(txtNamXuatBan.Text), txtNhaXuatBan.Text, Int32.Parse(txtTriGia.Text));
                if (KiemTraMaTacGia(serv.SelectMaTacGia(cmbTacGia.SelectedItem.ToString())) == false)
                {
                    m_insertInfo.InsertCTTacGia(serv.SelectMaTacGia(cmbTacGia.SelectedItem.ToString()), m_sMaDauSach);
                    m_deleteInfo.DeleteCTTacGia(m_sMaTacGia, m_sMaDauSach);
                }
                if (!m_bSelectData)
                    m_bSelectData = true;
                ClearIndex();
                dataSach.ItemsSource = null;
            }
        }

        private bool KiemTraMaTacGia(string MaTacGia)
        {
            for (int i = 0; i < serv.SelectMaTacGia_CTTG(m_sMaDauSach).Length; i++)
                if (MaTacGia == serv.SelectMaTacGia_CTTG(m_sMaDauSach)[i])
                {
                    MessageBox.Show("Trùng Tác Giả Hiện Tại", "Thông Báo");
                    return true;
                }
            return false;
        }

        //private void bntRefresh_Click(object sender, RoutedEventArgs e)
        //{
        //    //int zero = 0;
        //    //if (!m_bSelectData)
        //    //    m_bSelectData = true;
        //    //txtMaSach.Text = "";
        //    //cmbTacGia.SelectedItem = null;
        //    //cmbTheLoai.SelectedItem = null;
        //    //txtTenSach.Text = "";
        //    //txtNhaXuatBan.Text = "";
        //    //dtmNgayNhap.SelectedDate = null;
        //    //txtNamXuatBan.Text = zero.ToString();
        //    //txtTriGia.Text = zero.ToString();
        //    //dataSach.ItemsSource = null;
        //    //m_bSelectData = false;
        //    m_showInfo.ShowListSach(dataSach);
        //}
	}
}